<?php
include('header.php');
use Curl\Curl;
function stock(){
    global $db;
	global $root;
    $curl = new Curl();
    $curl->setUserAgent('Mozilla/5.0');
    $curl->get('http://stock.gtimg.cn/data/get_hs_xls.php?id=ranka&type=1&metric=chr');
    $file_name = $root.'/'.date('Ymd').'.xls';
    $dataSet = '';
    if ($curl->error) {
        echo $curl->error_code;
    }
    else {
        file_put_contents($file_name, $curl->response);
        //echo $curl->response;
        $objPHPExcel = \PHPExcel_IOFactory::load($file_name);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        $objdata = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        $date = str_replace('-', '', date('Y-m-d',strtotime(date('Y',time()).'-'.$objdata['1']['B'])));
        foreach ($objdata as $k=>$v){
            if($k>2){
                $dataSet[]=array(
                    'dm'=>$v['A'],
                    'name'=>$v['B'],
                    'zxj' =>$v['C'],
                    'zdf' =>str_replace('%', '', $v['D']),
                    'zde' =>$v['E'],
                    'cjl' =>$v['H'],
                    'cje' =>$v['I'],
                    'jk' =>$v['J'],
                    'zs' =>$v['K'],
                    'zg' =>$v['L'],
                    'zd' =>$v['M'],
                    'data_time'=>$date,
                    'ctime'=>date('Y-m-d H:i:s',time()),
                );
            }
        }
    }
    if($dataSet) {
        $db->where('data_time',$date);
        $db->delete('record');
        $db->insertMulti('record',$dataSet);
        echo 'success';
    }else{
        echo 'false';
    }
    $curl->close();
}

stock();
